/*
 * @Author: 肖燕 2461435122@qq.com
 * @Date: 2022-11-21 14:04:33
 * @LastEditors: 肖燕 2461435122@qq.com
 * @LastEditTime: 2025-02-19 16:25:19
 * @FilePath: /mrismodulecash/src/assets/scripts/tools/use-tools.js
 * @Description: 工具箱
 */
import Vue from 'vue'
import { isEmpty, merge } from 'lodash'
import { searchDefineTableData } from '@/services/apis/dict.js'

const vm = new Vue()

/**
 * @description: 弹窗
 * @return {*}
 */
class PopView {
  // 是否可见
  visible = false
  // 参数
  data = {}
  // 标题
  title = ''
  // 关闭弹窗
  close = () => {
    return (() => {
      this.visible = false
    })()
  }
  constructor (title, query = {}) {
    this.title = title
    if (!isEmpty(query)) {
      for (const [key, value] of Object.entries(query)) {
        this[key] = value
      }
    }
  }
  /**
   * @description: 显示弹窗
   * @param {*} data
   * @return {*}
   */
  show (data) {
    this.data = data || {}
    this.visible = true
  }
}

/**
 * @description: 窗口-表单
 * @return {*}
 */
class PopOpt extends PopView {
  ifEdit = false
  show (row) {
    if (row) {
      this.ifEdit = true
      this.title = '编辑'
    } else {
      this.ifEdit = false
      this.title = '新增'
    }
    this.visible = true
    this.data = row || {}
  }
}

/**
 * @description: 富文本操作
 * @return {*}
 */
class PopRichOpt extends PopView {
  // 富文本是否可编辑
  ifEdit = false
  // 富文本内容
  htmlData = ''
  handleChangeRich (htmlData) {
    // 改为相对路径存数据库
    const res = vm.$setHtmlWithPath(htmlData, '/', '/static')
    this.htmlData = res
  }
  customInsertFn ({ res, insertFn, editor }) {
    if (res.msg === 'SUCCESS') {
      // 从 res 中找到 url alt href ，然后插图图片
      const url = `${window.apiUrl.sys}/${res.data.file_name}`
      insertFn(url)
    }
  }
}

/**
 * @description: 导航切换
 * @return {*}
 */
class NavBar {
  subList = []
  subActive = ''
  activeLabel = ''
  props = {
    'label': 'label',
    'value': 'para',
    'count': 'count',
    'children': 'children'
  }
  constructor (list, active, props) {
    if (props) {
      ; ({
        label: this.props.label = 'label',
        value: this.props.value = 'para',
        count: this.props.count = 'count',
        children: this.props.children = 'children'
      } = props)
    }
    this.setValues(list, active)
  }
  /**
   * @description: 初始化数据
   * @return {*}
   */
  init () {
    this.active = this.list[0]?.[this.props.value] || ''
    this.activeLabel = this.list[0]?.[this.props.label] || ''
    this.setSubInit()
  }
  /**
   * @description: 设值
   * @param {*} list
   * @param {*} active
   * @param {*} props
   * @return {*}
   */
  setValues (list = [], active = '') {
    this.list = list
    this.active = active
    if (this.list.length) {
      this.init()
    }
  }
  /**
   * @description: 设置选中项
   * @param {*} active
   * @param {*} isSub
   * @return {*}
   */
  setActive (active = '', isSub = false) {
    if (isSub) {
      this.subActive = active
    } else {
      this.active = active
      this.setActiveLabel()
    }
  }
  /**
   * @description: 设置active的label值
   * @return {*}
   */
  setActiveLabel () {
    const selIndex = this.list.findIndex(item => item[this.props.value] === this.active)
    if (selIndex > -1) {
      this.activeLabel = this.list[selIndex][this.props.label]
    }
  }
  /**
   * @description: 默认选中sub nav
   * @param {*} index
   * @return {*}
   */
  setSubInit (index = 0) {
    this.subList = this.list[index]?.[this.props.children] || []
    this.subActive = this.subList[0]?.[this.props.value] || ''
  }
  /**
   * @description: 改变选项
   * @param {*} item
   * @param {*} active
   * @return {*}
   */
  change ({ item, activeName, subActive, isSub }) {
    if (isSub) {
      this.subActive = item[this.props.value]
    } else {
      this.active = activeName
      this.setActiveLabel()
      this.subActive = subActive || ''
      const selIndex = this.list.findIndex(tItem => tItem[this.props.value] === activeName)
      if (selIndex > -1) {
        this.setSubInit(selIndex)
      }
    }
  }
}

/**
 * @description: 表格类
 * @return {*}
 */
class TableItem {
  data = []
  selRow = null
  constructor ({ columns = [], paginationShow = false, height = 0, pageObj = null }) {
    this.columns = columns
    this.paginationShow = paginationShow
    this.height = height
    this.pageObj = pageObj
    if (paginationShow) {
      this.pageObj = this.setPage()
    }
  }
  /**
   * @description: 页码赋初值
   * @return {*}
   */
  setPage () {
    return {
      'pageSize': 25,
      'currentPage': 1,
      'total': 0,
      'layout': 'total, prev, pager, next, jumper'
    }
  }
  /**
   * @description: 改变页面
   * @param {*} size
   * @param {*} page
   * @return {*}
   */
  handlePageSize ({ size, page }) {
    this.pageObj.currentPage = page
    this.pageObj.pageSize = size
  }
  /**
   * @description: 单击行
   * @param {*} row
   * @return {*}
   */
  handleRowClick (row) {
    this.selRow = row
  }
  /**
   * @description: 重置页码
   * @return {*}
   */
  resetPage () {
    this.pageObj = this.setPage()
  }
  /**
   * @description: 高度自适应
   * @param {*} refItem
   * @return {*}
   */
  doLayout (refItem) {
    setTimeout(() => {
      this.height = vm.$handleTableHeight(refItem)
    }, 0)
  }
}

/**
 * @description: 筛选
 * @return {*}
 */
class SearchBlock {
  searchData = {}
  /**
   * @description: 构造函数
   * @param {*} list searchList
   * @param {*} data searchData
   * @return {*}
   */
  constructor (list = [], data = {}) {
    this.searchList = list
    this.init(data)
  }
  /**
   * @description: 初始化数据
   * @param {*} data
   * @return {*}
   */
  init (data = {}) {
    this.searchData = merge(this.setSearchData(), data)
  }
  /**
   * @description: 设默认值
   * @return {*}
   */
  setSearchData () {
    const res = {}
    this.searchList.forEach(item => {
      res[item.keyCode] = (['select', 'select-remote'].includes(item.type)) && item.multiple ? [] : ''
    })
    return res
  }
  /**
   * @description: 重置数据
   * @return {*}
   */
  reset () {
    const resetNotArr = this.searchList.filter(item => item.isResetNot)
    const res = {}
    if (resetNotArr.length) {
      resetNotArr.forEach(item => {
        res[item.keyCode] = this.searchData[item.keyCode]
      })
    }
    this.searchData = merge(this.setSearchData(), res)
  }
  /**
   * @description: 改变所有值
   * @param {*} values
   * @return {*}
   */
  setValues (values = {}) {
    this.searchData = values
  }
  /**
   * @description: 设置某个值
   * @param {*} keyCode
   * @param {*} value
   * @return {*}
   */
  setValue (keyCode, value) {
    this.searchData[keyCode] = value
  }
  /**
   * @description: 改变筛选值
   * @param {*} item
   * @param {*} value
   * @param {*} valueOrigin
   * @return {*}
   */
  change = (values, item, valueOrigin) => {
    return (() => {
      this.searchData = values
    })()
  }
}
/**
 * @description: 获取hospital表数据
 * @return {*}
 */
class TableList {
  tableName = ''
  data = []
  constructor (tableName = '', resPara = 'records', query = {}) {
    this.tableName = tableName
    this.resPara = resPara
    this.query = query
    tableName && this.init()
  }
  /**
   * @description: 初始化数据
   * @param {*}
   * @return {*}
   */
  async init () {
    const res = await searchDefineTableData(this.tableName, this.query)
    this.data = res[this.resPara] || []
  }
}

export {
  PopView,
  PopOpt,
  PopRichOpt,
  NavBar,
  TableItem,
  SearchBlock,
  TableList
}
